package com.dsteiner.xelavos.client;

import java.beans.PropertyChangeListener;

import com.dsteiner.widgets.client.PrefilledTextArea;
import com.dsteiner.widgets.client.PropertyChangeCollection;
import com.dsteiner.widgets.client.Widgets.ButtonExt;
import com.dsteiner.widgets.client.Widgets.HTMLExt;
import com.dsteiner.widgets.client.Widgets.LazyWidget;
import com.dsteiner.widgets.client.model.PrefilledTextBoxModel;
import com.dsteiner.xelavos.client.controller.SendMessageController;
import com.dsteiner.xelavos.client.model.AuthenticationModel;
import com.dsteiner.xelavos.client.resources.XelavosResources;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.Widget;

public class SendMessageWidget extends LazyWidget {

	public static class SendMessageModel {
		private PropertyChangeCollection propertyChangeSupport = new PropertyChangeCollection(this);

		public void addPropertyChangeListener(String propertyName, PropertyChangeListener listener) {
			propertyChangeSupport.registerPropertyChangeListener(propertyName, listener);
		}
		
		private String toUserName;

		public String getToUserName() {
			return toUserName;
		}

		public void setToUserName(String toUserName) {
			String oldToUserName = this.toUserName;
			this.toUserName = toUserName;
			propertyChangeSupport.firePropertyChange("toUserName", oldToUserName, toUserName);
		}
		
		private String toUserId;

		public String getToUserId() {
			return toUserId;
		}

		public void setToUserId(String toUserId) {
			String oldToUserId = this.toUserId;
			this.toUserId = toUserId;
			propertyChangeSupport.firePropertyChange("toUserId", oldToUserId, toUserId);
		}
	}

	private SendMessageController sendMessageController = new SendMessageController();
	private final SendMessageModel sendMessageModel;
	private final AuthenticationModel authenticationModel;

	public SendMessageWidget(SendMessageModel sendMessageModel, AuthenticationModel authenticationModel) {
		this.sendMessageModel = sendMessageModel;
		this.authenticationModel = authenticationModel;
		
		setStyleName(XelavosResources.INSTANCE.css().sendMessageWidget());
	}

	@Override
	public Widget createContentWidget() {
		FlowPanel mainPanel = new FlowPanel();

		PrefilledTextBoxModel messageTextAreaModel = new PrefilledTextBoxModel();
		PrefilledTextArea messageTextArea = new PrefilledTextArea(messageTextAreaModel);
		messageTextAreaModel.setDefaultText("escribe su mensaje aqui");
		ButtonExt sendMessageButton = new ButtonExt("mandar mensaje");
		HTMLExt titleHtml = new HTMLExt();
		
		mainPanel.add(titleHtml);
		mainPanel.add(messageTextArea);
		mainPanel.add(sendMessageButton);
		
		sendMessageController.registerTitleHtml(titleHtml);
		sendMessageController.registerAuthenticationModel(authenticationModel);
		sendMessageController.registerSendMessageButton(sendMessageButton);
		sendMessageController.registerMessageTextAreaModel(messageTextAreaModel);
		sendMessageController.registerSendMessageWidget(this);
		sendMessageController.registerSendMessageModel(sendMessageModel);
		return mainPanel;
	}

}